package com.xingzhi.shortvideosharingplatform.controller;

import com.xingzhi.shortvideosharingplatform.common.Result;
import com.xingzhi.shortvideosharingplatform.dto.VideoFavoriteDTO;
import com.xingzhi.shortvideosharingplatform.service.SystemNotificationService;
import com.xingzhi.shortvideosharingplatform.service.UserBehaviorLogsService;
import com.xingzhi.shortvideosharingplatform.service.VideoFavoriteService;
import com.xingzhi.shortvideosharingplatform.service.VideoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/favorites/video")
public class VideoFavoriteController {

    @Autowired
    private VideoFavoriteService videoFavoriteService;
    
    @Autowired
    private UserBehaviorLogsService behaviorLogsService;
    
    @Autowired
    private SystemNotificationService notificationService;
    
    @Autowired
    private VideoService videoService;
    
    /**
     * 添加视频到收藏夹，支持一次添加到多个收藏夹
     */
    @PostMapping("/add")
    public Result<Boolean> addVideoToFolder(@RequestBody VideoFavoriteDTO favoriteDTO) {
        try {
            // TODO: 从Token中获取用户ID
            // 检查folderIds是否为空
            if (CollectionUtils.isEmpty(favoriteDTO.getFolderIds())) {
                return Result.error(400, "收藏夹ID不能为空");
            }
            boolean success = videoFavoriteService.addVideoToFolder(favoriteDTO);
            return Result.success(success, "收藏成功");
        } catch (Exception e) {
            log.error("收藏视频失败", e);
            return Result.error(500, "收藏视频失败: " + e.getMessage());
        }
    }
    
    /**
     * 从特定收藏夹中取消收藏
     */
    @DeleteMapping("/remove")
    public Result<Boolean> removeFromFolder(@RequestParam Long userId, @RequestParam Long videoId, @RequestParam Long folderId) {
        try {
            // TODO: 从Token中获取用户ID
            boolean success = videoFavoriteService.removeVideoFromFolder(userId, videoId, folderId);
            if (success) {
                return Result.success(true, "取消收藏成功");
            } else {
                return Result.error(400, "取消收藏失败，记录不存在");
            }
        } catch (Exception e) {
            log.error("取消收藏失败", e);
            return Result.error(500, "取消收藏失败: " + e.getMessage());
        }
    }
    
    /**
     * 取消视频的所有收藏（从所有收藏夹中取消）
     */
    @GetMapping("/removeAll")
    public Result<Boolean> removeAllFavorites(@RequestParam Long userId, @RequestParam Long videoId) {
        try {
            // TODO: 从Token中获取用户ID
            boolean success = videoFavoriteService.removeAllVideoFavorites(userId, videoId);
            if (success) {
                return Result.success(true, "取消所有收藏成功");
            } else {
                return Result.error(400, "取消收藏失败，记录不存在");
            }
        } catch (Exception e) {
            log.error("取消所有收藏失败", e);
            return Result.error(500, "取消所有收藏失败: " + e.getMessage());
        }
    }
    
    /**
     * 获取收藏夹中的视频列表
     */
    @GetMapping("/list/{folderId}")
    public Result<List<Long>> getVideosByFolderId(@PathVariable Long folderId) {
        try {
            List<Long> videoIds = videoFavoriteService.getVideoIdsByFolderId(folderId);
            return Result.success(videoIds, "获取收藏视频列表成功");
        } catch (Exception e) {
            log.error("获取收藏夹视频列表失败", e);
            return Result.error(500, "获取收藏夹视频列表失败: " + e.getMessage());
        }
    }
    
    /**
     * 检查视频是否已被收藏到指定收藏夹
     */
    @GetMapping("/check")
    public Result<Boolean> isVideoInFolder(@RequestParam Long userId, @RequestParam Long videoId, @RequestParam Long folderId) {
        try {
            // TODO: 从Token中获取用户ID
            boolean isFavorite = videoFavoriteService.isVideoInFolder(userId, videoId, folderId);
            return Result.success(isFavorite, "检查收藏状态成功");
        } catch (Exception e) {
            log.error("检查视频收藏状态失败", e);
            return Result.error(500, "检查视频收藏状态失败: " + e.getMessage());
        }
    }
    
    /**
     * 检查视频是否已被用户收藏（不考虑收藏夹）
     */
    @GetMapping("/check/simple")
    public Result<Boolean> isVideoFavorited(@RequestParam Long userId, @RequestParam Long videoId) {
        try {
            // TODO: 从Token中获取用户ID
            boolean isFavorite = videoFavoriteService.isVideoFavorited(userId, videoId);
            return Result.success(isFavorite, "检查收藏状态成功");
        } catch (Exception e) {
            log.error("检查视频收藏状态失败", e);
            return Result.error(500, "检查视频收藏状态失败: " + e.getMessage());
        }
    }
    
    /**
     * 获取视频所在的所有收藏夹ID
     */
    @GetMapping("/folders")
    public Result<List<Long>> getFoldersByVideo(@RequestParam Long userId, @RequestParam Long videoId) {
        try {
            // TODO: 从Token中获取用户ID
            List<Long> folderIds = videoFavoriteService.getFolderIdsByUserAndVideo(userId, videoId);
            return Result.success(folderIds, "获取视频所在收藏夹列表成功");
        } catch (Exception e) {
            log.error("获取视频所在收藏夹列表失败", e);
            return Result.error(500, "获取视频所在收藏夹列表失败: " + e.getMessage());
        }
    }
    
  
} 